<template>
    <div>
        <div
            class="ccui-multifunctional-table-wrapper"
            style="marign-top: 20px"
        >
            <ux-grid
                align="center"
                ref="multipleTable"
                :data="tableData"
                border
                class="ccui-multifunctional-table"
                lazy
                show-summary
                :summary-method="getSummaries"
                keep-source
                max-height="9999"
                show-overflow
                :highlightCurrentRow="false"
                :widthResize="true"
                :edit-config="{ trigger: 'click', mode: 'cell' }"
            >
                <!-- <ux-table-column type="checkbox" width="55" fixed="left" /> -->
                <ux-table-column
                    type="index"
                    title="序号"
                    width="80"
                    fixed="left"
                />
                <ux-table-column
                    title="批次号"
                    field="batchNumber"
                    width="200"
                    fixed="left"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="设备物资编码"
                    field="materialCode"
                    width="110"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="设备物资名称"
                    field="materialName"
                    width="260"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="设备物资说明"
                    field="materialState"
                    width="130"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="分类名称"
                    field="categoryName"
                    width="170"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="计量单位"
                    field="measureUnit"
                    width="90"
                    show-overflow-tooltip
                />
                <ux-table-column
                    title="库存数量"
                    field="storageCount"
                    width="120"
                    header-align="left"
                />
                <ux-table-column
                    title="可用库存数量"
                    field="unStorageCount"
                    width="130"
                    header-align="left"
                />
                <ux-table-column
                    title="移库数量"
                    field="transferLibraryCount"
                    width="130"
                />
                <ux-table-column
                    title="币种"
                    field="currencyName"
                    width="100"
                    header-align="left"
                />
                <ux-table-column
                    title="汇率"
                    field="exchangeRate"
                    width="100"
                    header-align="left"
                />
                <ux-table-column
                    title="含税单价"
                    field="unitPriceWithTax"
                    width="100"
                    align="right"
                    header-align="left"
                />
                <ux-table-column
                    title="含税金额"
                    field="moneyWithTax"
                    width="100"
                    align="right"
                    header-align="left"
                />
                <ux-table-column
                    title="备注"
                    field="remark"
                    width="200"
                />
            </ux-grid>
        </div>
    </div>
</template>

<script>
import { formatNumber } from "@/basa/utils";
export default {
    props: {
        detailPurchaseData: {
            type: Array,
            default: () => [],
        },
    },
    data() {
        return {
            tableData: [],
        };
    },
    watch: {
        detailPurchaseData: {
            handler(data) {
                this.tableData = data;
                console.log(this.tableData)
            },
            immediate: true
        },
    },
    methods: {
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计";
                    return;
                }
                if (["storageCount","unStorageCount","transferLibraryCount","moneyWithTax"].includes(column.property)) {
                    const values = data.map((item) =>
                        Number(item[column.property])
                    );
                    if (!values.every((value) => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                let num = Number(prev) + Number(curr);
                                return num;
                            } else {
                                return Number(prev);
                            }
                        }, 0);
                       if (column.title == "库存数量") {
                            sums[index] = sums[index]
                                ? formatNumber(Number(sums[index]), 3)
                                : "";
                        }
                        if (column.title == "移库数量") {
                            sums[index] = sums[index]
                                ? formatNumber(Number(sums[index]), 3)
                                : formatNumber(0, 3);
                                console.log(sums[index])
                            this.$bus.$emit('getMoveNum',Number(sums[index]))
                        }
                        if (column.title == "可用库存数量") {
                            sums[index] = sums[index]
                                ? formatNumber(Number(sums[index]), 3)
                                : "";
                        }
                        if (column.title == "含税金额") {
                            sums[index] = sums[index]
                                ? Number(sums[index]).toFixed(2)
                                : "";
                            this.$bus.$emit('getMoveMoney',Number(sums[index]))
                        }


                    }
                }
                
            });
            return [sums];
        },
    },
};
</script>

<style lang="scss" scoped>
/deep/ .elx-table .elx-footer--column{
    font-weight:bolder;
}
</style>